package com.abluedog.c1;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import static com.abluedog.util.ByteBufferUtil.debugAll;

public class ByteBufferStringTest {

    public static void main(String[] args) {
        //1. 字符转字节数组
        String str = "abcde";
        byte[] data = str.getBytes();
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.put(data);
        debugAll(buffer);

        //2.charset 自动切换到读模式
        ByteBuffer encode = StandardCharsets.UTF_8.encode(str);
        debugAll(encode);

        //wrap 自动切换到读模式
        ByteBuffer wrap = ByteBuffer.wrap(data);
        debugAll(wrap);
    }
}
